package com.dhcc.bpm.alarm.utils;



import com.jcraft.jsch.SftpProgressMonitor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.NumberFormat;


/**
 * @author kevin.chen
 */
public class UploadMonitor implements SftpProgressMonitor{
    private static Logger LOGGER = LoggerFactory.getLogger(UploadMonitor.class);
    /**
     * 文件的总大小
     */
    private long maxCount = 0;
    private long uploaded = 0;
    long startTime = 0L;
    private String caseName;
    public UploadMonitor(String caseName,long maxCount) {
        this.maxCount = maxCount;
        this.caseName = caseName;

    }
    /**
     * 当文件开始传输时，调用init方法
     *
     * @param op
     * @param src
     * @param dest
     * @param max
     */
    @Override
    public void init(int op, String src, String dest, long max) {
        LOGGER.info("开始上传文件：" + src + "至远程：" + dest + "文件总大小:" + maxCount / 1024 + "KB");
//        this.maxCount = max;
        startTime = System.currentTimeMillis();
    }

    /**
     * 当每次传输了一个数据块后，调用count方法，count方法的参数为这一次传输的数据块大小
     *
     * @param count
     * @return
     */
    @Override
    public boolean count(long count) {
        uploaded += count;
//        LOGGER.info("本次上传大小：" + count / 1024 + "KB,");
        run();
        if (count > 0) {
            return true;
        }
        return false;
    }
    /**
     * 当传输结束时，调用end方法
     */
    @Override
    public void end() {
        if(uploaded<maxCount){
            LOGGER.info("传输失败");
        }else {
            LOGGER.info("传输成功");
        }
    }
    public String status(){
        String code=null;
        if(uploaded<maxCount){
            code ="false";
        }else {
            code = "true";
        }
        return code;
    }
    public void run() {
        NumberFormat format = NumberFormat.getPercentInstance();
        format.setMaximumFractionDigits(2);
        format.setMinimumFractionDigits(2);
        String value = format.format((uploaded / (double) maxCount)/2+0.5);
    }
}
